package cc.idoone.spring_boot_hello.amqp.topic;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 路由匹配模式
 *
 * @author hemj
 * @since 2017/10/15
 */
@Configuration
public class TopicConfig {

	static final String DIRECT_KEY = "spring-boot.key";
	static final String TOPIC_KEY = "spring-boot.#"; //*匹配一个单词 #匹配多个单词
	static final String QUENE = "spring-boot-queue2";
	static final String EXCHANGE = "spring-boot-exchange1";

	@Bean(name = "queue2")
	Queue queue() {
		// queue 的 durable 属性的值定义后不可以修改，若修改则 queue 需要另起名称
		return new Queue(QUENE, true);
	}

	@Bean(name = "binding2")
	Binding binding(@Qualifier("queue") Queue queue, @Qualifier("exchange") TopicExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange).with(DIRECT_KEY);
	}

	@Bean(name = "topicBinding")
	Binding topicBinding(@Qualifier("queue2") Queue queue, @Qualifier("exchange") TopicExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange).with(TOPIC_KEY);
	}

	@RabbitListener(queues = "spring-boot-queue2")
	public void receiveQueue(String message) {
		System.out.println("Queue2: " + message);
	}

}
